package com.fieldchain.ex.fa100.utils.common;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.fieldchain.ex.fa100.inter.BooleanCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/10
 */
public class EditTextUtils {
    private List<InputEntry> list = new ArrayList<>();
    private boolean isOk[] = null;

    public void append(EditText et, RelativeLayout rea) {
        InputEntry entry = new InputEntry();
        entry.setEt(et);
        entry.setRea(rea);
        list.add(entry);
    }

    public void start(final BooleanCallBack callBack) {
        isOk = new boolean[list.size()];
        for (int i = 0; i < list.size(); i++) {
            final EditText et = list.get(i).getEt();
            final RelativeLayout rea = list.get(i).getRea();
            final int finalI = i;
            rea.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    et.setText("");
                }
            });
            listener(et, new BooleanCallBack() {
                @Override
                public void status(boolean status) {
                    isOk[finalI] = status;
                    callBack.status(checkIsok());
                    if (status) {
                        rea.setVisibility(View.VISIBLE);
                    }else{
                        rea.setVisibility(View.GONE);
                    }
                }
            });
        }
    }

    public void start() {
        isOk = new boolean[list.size()];
        for (int i = 0; i < list.size(); i++) {
            final EditText et = list.get(i).getEt();
            final RelativeLayout rea = list.get(i).getRea();
            final int finalI = i;
            rea.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    et.setText("");
                }
            });
            listener(et, new BooleanCallBack() {
                @Override
                public void status(boolean status) {
                    if (status) {
                        rea.setVisibility(View.VISIBLE);
                    }else{
                        rea.setVisibility(View.GONE);
                    }
                }
            });
        }
    }

    public void cancle() {
        list.clear();
    }


    public void listener(EditText et, final BooleanCallBack listener) {
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().length() > 0) {
                    listener.status(true);
                } else {
                    listener.status(false);
                }
            }
        };
        et.addTextChangedListener(watcher);
    }

    public boolean checkIsok() {
        for (int i = 0; i < isOk.length; i++) {
            if (isOk[i] = false) {
                return false;
            }
        }
        return true;
    }
}
